$(function () {
    sendMsgAsync(realpath + "/index/navtext.do", "action=getNavXml", createNavText);
    $("#head tr td div").first().css("background", "red").attr("highlight", "true");
    $("#head tr td div").hover(function () {
        var ishighlight = $(this).attr("highlight");
        if (ishighlight == "true") {
            return;
        }
        $(this).css('background', "red");
    }, function () {
        var ishighlight = $(this).attr("highlight");
        if (ishighlight != "true") {
            $(this).css('background', "white");
        }
    });
    var userdiv = $(".nav_user");
    var login = $("<a href='javascript:;'>").html("登录").click(loginPage);
    var reg = $("<a  href='javascript:;'>").html("注册").click(regPage);
    var unlogin = $("<div class='nav_unlogin'>").append(login).append(reg).appendTo(userdiv);
    var usernamespan = $("<div class='nav_username_div'>");
    var username = $("<a  class='nav_username' href='javascript:;'>").text("请登录").appendTo(usernamespan);
    var usersettingdiv=$("<div class='nav_usersetting'>").appendTo(usernamespan);
    var usersarray=["个人中心","修改密码","注销"];
    createUserSetting(usersettingdiv,usersarray,[null,null,loginout]);

    usernamespan.hover(function () {
    	$(this).css({"height":(usersarray.length*25)});
        $(".nav_usersetting").css({'display': 'block'}).slideDown("fast");
    }, function () {
    	$(this).css('height',"20px");
     $(".nav_usersetting").css('display', 'none').slideUp("fast");
    });

    var welspan = $("<span class='nav_welcometext'>").html("&nbsp;");
    var logined = $("<div class='nav_logined'>").append(usernamespan).append(welspan).appendTo(userdiv);
    checkIsLogin();
    setInterval(checkIsLogin, 200 * 1000);
});
function change(e) {
    $("#head tr td div").attr("highlight", "false");
    e.setAttribute("highlight", "true");
    $("#head tr td div").css('background', "white");
    ;
    e.style.background = "red";
    $("#body iframe").css("display", 'none');
    //$("#body iframe").css("display",'');
    $("#" + e.getAttribute("frameid")).css("display", "");
}
function createNavText(msg) {
    var xmlDoc = $.parseXML(msg);
    var parent = $("#head table tr");
    $(xmlDoc).find("nav").each(function (i) {
        var div = $("<div onclick='change(this)'>").attr({"frameid": "nav" + i, "class": $(this).attr("class")}).text($(this).text());
        $("<td class='nav_td'>").appendTo(parent).append(div);
        var iframe = $("<iframe>").attr({"id": "nav" + i, "class": $(this).attr("iframeClass"), "src": $(this).attr("src")});
        if (i != 0) {
            iframe.css("display", "none");
        }
        $("#body").append(iframe);
    });
}

function checkIsLogin() {
    sendMsgSync(realpath + "/index/checklogin.do", "", repairIcon);
}
function repairIcon(msg) {
    if (msg == "false") {
        $(".nav_unlogin").css("display", "");
        $(".nav_logined").css("display", "none");
    } else {
        var jsonobj = JSON.parse(msg);
        var res=calWidth(jsonobj.name);
        $(".nav_username_div").css("width",res[0]);
        var a = $(".nav_username").html(jsonobj.name);
        var awidth=a.width();;
        $(".nav_usersetting").css("right",res[1]);
        $(".nav_welcometext").text("欢迎您");
        $(".nav_unlogin").css("display", "none");
        $(".nav_logined").css("display", "");
    }
}
function loginPage() {
    var logindia = new Dialog(window, "登录");
    logindia.setWidth("350px");
    logindia.setHeight("180px");
    var contentdiv = logindia.getContent();
    var info=$("<span class='login_info_right'>").appendTo(contentdiv).text("请输入账号密码");
    var table = $("<table class='login_table'>")[0];
    var tr1 = table.insertRow(-1);
    var td11 = tr1.insertCell(-1);
    td11.innerText = "账号:";
    var td12 = tr1.insertCell(-1);
    var username = $("<input name='name'>").appendTo(td12);
    var tr2 = table.insertRow(-1);
    var td21 = tr2.insertCell(-1);
    td21.innerText = "密码:";
    var td22 = tr2.insertCell(-1);
    var pwd = $("<input name='pwd' type='password'>").appendTo(td22);
    var tr3 = table.insertRow(-1);
    var td31 = tr3.insertCell(-1);
    td31.innerText = "验证码:";
    var td32 = tr3.insertCell(-1);
    var verifycode=$("<input>").appendTo(td32);
    var td33 = tr3.insertCell(-1);
    var veryimg=$("<img>").appendTo(td33).attr("src", realpath+"/index/verify.do").click(function(){
    	$(this).attr("src", realpath+"/index/verify.do?a="+new Date());
    });
    contentdiv.appendChild(table);
    logindia.addButton("登录", 20, 5, function () {
        var uesrnameval = username.val();
        var pwdval = pwd.val();
        var vercode=verifycode.val();
        var map=new Map();
    		map.put("id",uesrnameval);
    		map.put("pwd",pwdval);
    		map.put("code",vercode);
        sendMsgSync(realpath + "/index/login.do", map, function (msg) {
        	 var jsonobj = JSON.parse(msg);
        	 if(jsonobj.state=="true"){
        		 checkIsLogin();
             logindia.close();
        	 }else{
        		 info.attr("class","login_info_wrong");
        		 info.text(jsonobj.msg);
        		 veryimg.attr("src", realpath+"/index/verify.do?a="+new Date());
        	 }
        });
    });
    logindia.addButton("取消", 5, 5, function () {
        logindia.close();
    });
    logindia.showModel();
}
function regPage() {
    var logindia = new Dialog(window, "注册");
    logindia.setWidth("380px");
    logindia.setHeight("200px");
    var contentdiv = logindia.getContent();
    var info=$("<span class='login_info_right'>").appendTo(contentdiv).text("请输入账号密码");
    var table = $("<table class='login_table'>")[0];
    var tr1 = table.insertRow(-1);
    var td11 = tr1.insertCell(-1);
    td11.innerText = "账号:";
    var td12 = tr1.insertCell(-1);
    var username = $("<input name='name'>").appendTo(td12);
    var td13 = tr1.insertCell(-1);
    var span=$("<span>").appendTo(td13);
    $("<input type='button' value='检测用户名'>").appendTo(span).click(function(){
    	
    	var usernameval=username.val();
    	if(usernameval&&usernameval!=""){
    		var map=new Map();
      	map.put("id",usernameval);
      	sendMsgSync(realpath+"/index/checkid.do", map, function(msg){
      	  var jsonobj = JSON.parse(msg);
      	  if(jsonobj.state=="true"){
      	  	info.attr("class","login_info_right");
      	  	info.text("用户名可以使用");
      	  }else{
      	  	info.attr("class","login_info_wrong");
      	  	info.text("用户名已经存在");
      	  }
      	});
    	}else{
    		info.attr("class","login_info_wrong");
    		info.text("用户名不能为空");
    	}
    });
    var tr2 = table.insertRow(-1);
    var td21 = tr2.insertCell(-1);
    td21.innerText = "密码:";
    var td22 = tr2.insertCell(-1);
    var pwd = $("<input name='pwd' type='password'>").appendTo(td22);
    var td23 = tr2.insertCell(-1);
    $("<span>").appendTo(td23);
    var tr3 = table.insertRow(-1);
    var td31 = tr3.insertCell(-1);
    td31.innerText = "确认密码:";
    var td32 = tr3.insertCell(-1);
    var pwd2=$("<input type='password'>").appendTo(td32);
    var td33 = tr3.insertCell(-1);
    $("<span>").appendTo(td33);
    var tr4 = table.insertRow(-1);
    var td41 = tr4.insertCell(-1);
    td41.innerText = "验证码:";
    var td42 = tr4.insertCell(-1);
    var verifycode=$("<input>").appendTo(td42);
    var td43 = tr4.insertCell(-1);
    $("<img>").appendTo(td43).attr("src", realpath+"/index/verify.do").click(function(){
    	$(this).attr("src", realpath+"/index/verify.do?a="+new Date());
    });
    contentdiv.appendChild(table);
    logindia.addButton("注册", 20, 5, function(){
    	var usernameval=username.val();
    	var pwdval1=pwd.val();
    	var pwdvar2=pwd2.val();
    	var vercode=verifycode.val();
    	if(checkInputInfo(usernameval,pwdval1,pwdvar2)){
    		var map=new Map();
    		map.put("id",usernameval);
    		map.put("pwd",pwdval1);
    		map.put("code",vercode);
    		sendMsgSync(realpath+"/index/reg.do",map,function(msg){
    			logindia.close();
    			showWaitDialog("正在登录");
    			checkIsLogin();
    			hideWaitDialog();
    		});
    	};
    	
    });
    logindia.addButton("取消", 5, 5, function () {
        logindia.close();
    });
    logindia.showModel();
}

function loginout(){
	var r=confirm("确定注销吗？");
	if(!r) return;
    sendMsgSync(realpath+"/index/loginout.do","",function(msg){
      repairIcon("false");
    });
}

function  createUserSetting(parentdom,array,funcs){
    var table=$("<table class='nav_usersetting_table'>").appendTo(parentdom);

 for(var i=0;i<array.length;i++){
     var tr=$("<tr>").appendTo(table);
     var td=$("<td>").appendTo(tr);
     var a=$("<a href='javascript:;'>").text(array[i]).appendTo(td);
     a[0].onclick=funcs[i];
 }
}

function checkInputInfo(name,pwd1,pwd2){
	if(name==""||name==null){
		alert("用户名不能为空");
		return false;
	}
	if(pwd1==""||pwd1==null){
		alert("密码不能为空");
		return false;
	}
	if(pwd1!=pwd2){
		alert("两次密码必须一致");
		return false;
	}
	return true;
}
/**
 * 根据名称长度调宽度
 * @param msg
 * @returns {Array}
 */
function calWidth(msg){
	var res=[];
	var width=0;
	var len=msg.length;
	if(len<=7){
		width=70;
		res=[width,-5];
		return res;
	}
	if(len<=12){
		width=120;
		res=[width,20];
		return res;
	}
	res=[150,30];
	return 	res;
}